package com.franklin.java.configuration.sql.completion.ui;

import com.franklin.java.api.ui.jpanel.AbstractComponent;
import com.franklin.java.api.ui.jpanel.StepBackLabel;
import com.franklin.java.api.ui.jpanel.checkbox.MultipleForOneCheckBox;
import com.franklin.java.api.ui.jpanel.color.ColorSelector;
import com.franklin.java.configuration.sql.completion.ColorState;
import com.franklin.java.configuration.sql.completion.CompletionSettingState;
import com.franklin.java.configuration.sql.completion.TableNamePrefixState;
import com.franklin.java.entity.sql.ShowDescriptionType;
import com.franklin.plugins.common.utils.DataCheckUtils;
import com.intellij.openapi.project.Project;
import com.intellij.ui.TitledSeparator;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.stream.Collectors;


/**
 * @author Franklin
 * @since 2021/2/19 16:08
 */
public class CompletionComponent extends AbstractComponent {

    private Project myProject;

    private JPanel mainPanel;
    private JBCheckBox isUseCamelCheckBox;
    private MultipleForOneCheckBox descriptionCheckBox;
    private JBTextField tablePrefixField;
    private JBCheckBox isShowEntityDescriptionCheckBox;
    private ColorSelector colorSelector;

    public CompletionComponent(Project project) {
        this.myProject = project;
        isUseCamelCheckBox = new JBCheckBox("Use camel case in completion ?");
        isUseCamelCheckBox.setSelected(CompletionSettingState.isUseCamel(myProject));

        descriptionCheckBox = new MultipleForOneCheckBox(
            ShowDescriptionType.getDescriptions(),
            CompletionSettingState.getDescription(myProject)
        );
        descriptionCheckBox.addTail("                                                         ");

        tablePrefixField = new JBTextField(TableNamePrefixState.getPrefix(myProject));
        tablePrefixField.setSize(new Dimension(50,30));

        this.isShowEntityDescriptionCheckBox = new JBCheckBox("Show property comment in get/set method completion ?");
        this.isShowEntityDescriptionCheckBox.setSelected(CompletionSettingState.isShowEntityComment(myProject));

        colorSelector = new ColorSelector(
            "You can choose the color you like to draw in completion results.",
            ColorState.getColor(myProject)
        );

        mainPanel = FormBuilder.createFormBuilder()
            //转驼峰
            .addComponent(new TitledSeparator("Column name transform"),1)
            .addComponent(new StepBackLabel("Completion columns with camel case ?"),1)
            .addComponent(isUseCamelCheckBox,1)
            //字段描述选择
            .addComponent(new TitledSeparator("Column description"),1)
            .addComponent(new StepBackLabel("Which column description do you want to use ?"),1)
            .addComponent(descriptionCheckBox,1)
            //解析实体前缀
            .addComponent(new TitledSeparator("Find column comment in entity class"),1)
            .addComponent(new StepBackLabel("If your table names have a prefix which your entity class don't have."),1)
            .addComponent(new StepBackLabel("Please fill it into the text field below, it can help me to find your column's comment in entity class."),1)
            .addComponent(new StepBackLabel("(If you multiple prefix to remove, please use ';' to split them)"),1)
            .addComponent(tablePrefixField,1)
            //实体方法注释提醒
            .addComponent(new TitledSeparator("Show comment for entity's getter and setter"),1)
            .addComponent(new StepBackLabel("Helps you easier to figure out the meaning of this property."),1)
            .addComponent(isShowEntityDescriptionCheckBox,1)
            //颜色设置
            .addComponent(new TitledSeparator("Completion color setting"))
            .addComponent(colorSelector,1)
            .addComponentFillVertically(new JPanel(),0)
            .getPanel();

    }

    @Override
    public JPanel getMainPanel() {
        return mainPanel;
    }

    public boolean isUseCamel(){
        return isUseCamelCheckBox.isSelected();
    }

    public boolean isShowEntityComment(){
        return isShowEntityDescriptionCheckBox.isSelected();
    }

    public String getSelectedDescription(){
        return descriptionCheckBox.getSelectItem();
    }

    public String getTablePrefix(){
        String text = tablePrefixField.getText();
        if (DataCheckUtils.isNotEmpty(text)){
            if (text.contains(";")) {
                String[] split = text.split(";");
                text = Arrays.stream(split)
                    .filter(StringUtils::isNotBlank)
                    .collect(Collectors.joining(";"));
            }
        }else {
            text = "";
        }
        return text;
    }

    public Color getSelectColor(){
        return colorSelector.getColor();
    }
}
